git 如何stash(储藏)部分文件

您所在的位置:网站首页 git add添加多个文件 git 如何stash(储藏)部分文件

git 如何stash(储藏)部分文件

2023-11-14 13:06| 来源: 网络整理| 查看: 265

今天工作时遇到一个问题,新做的功能有问题,加了好多调试信息,另外想回退到上一次的版本看一下运行时的代码堆栈有什么不同,这就需要把加的调试信息stash起来,不然回退的时候会冲突。但是目录下还有好多被IDE修改的文件不想动,只能stash特定的这几个文件。于是就去读git help stash,果然git stash有此功能,只不过之前没用过。

大家都知道git stash,但是知道git stash是git stash push命令省略写法的人不多,git stash push指令本身就支持指定文件。

$ git stash push [file4 ...]

详细可参考git help stash中的push章节,如下:

push [-p|--patch] [-k|--[no-]keep-index] [-u|--include-untracked] [-a|--all] [-q|--quiet] [-m|--message ] [--] […​]

Save your local modifications to a new stash entry and roll them back to HEAD (in the working tree and in the index). The part is optional and gives the description along with the stashed state.

For quickly making a snapshot, you can omit “push”. In this mode, non-option arguments are not allowed to prevent a misspelled subcommand from making an unwanted stash entry. The two exceptions to this are stash -p which acts as alias for stash push -p and pathspecs, which are allowed after a double hyphen – for disambiguation.

When pathspec is given to git stash push, the new stash entry records the modified states only for the files that match the pathspec. The index entries and working tree files are then rolled back to the state in HEAD only for these files, too, leaving files that do not match the pathspec intact.

If the --keep-index option is used, all changes already added to the index are left intact.

If the --include-untracked option is used, all untracked files are also stashed and then cleaned up with git clean, leaving the working directory in a very clean state. If the --all option is used instead then the ignored files are stashed and cleaned in addition to the untracked files.

With --patch, you can interactively select hunks from the diff between HEAD and the working tree to be stashed. The stash entry is constructed such that its index state is the same as the index state of your repository, and its worktree contains only the changes you selected interactively. The selected changes are then rolled back from your worktree. See the “Interactive Mode” section of git-add(1) to learn how to operate the --patch mode.

The --patch option implies --keep-index. You can use --no-keep-index to override this.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3